package com.huike.clues.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.huike.clues.domain.SysUser;
import com.huike.clues.domain.TbAssignRecord;
import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.domain.dto.ClueAssignmentDTO;
import com.huike.clues.domain.dto.TbClueAddDTO;
import com.huike.clues.domain.dto.TbCluePageDTO;
import com.huike.clues.domain.dto.TbCluePoolPageDTO;
import com.huike.clues.domain.vo.CluePageVo;
import com.huike.clues.mapper.*;
import com.huike.clues.service.TbClueService;
import com.huike.common.core.domain.entity.SysUserDTO;
import com.huike.common.exception.BaseException;
import com.huike.common.utils.SecurityUtils;
import com.huike.common.utils.bean.BeanUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 93238
 * @description 针对表【tb_clue(线索)】的数据库操作Service实现
 * @createDate 2023-10-12 06:35:46
 */
@Service
@RequiredArgsConstructor
public class TbClueServiceImpl extends ServiceImpl<TbClueMapper, TbClue>
        implements TbClueService {

    private final TbClueMapper tbClueMapper;

    private final TbActivityMapper tbActivityMapper;
    private final TbAssignRecordMapper tbAssignRecordMapper;
    private final TbClueTrackRecordMapper tbClueTrackRecordMapper;
    private final SysUserMapper sysUserMapper;

    @Override
    public Page<CluePageVo> findClueList(TbCluePageDTO tbCluePageDTO, LocalDate beginCreateTime, LocalDate endCreateTime) {
        PageHelper.startPage(tbCluePageDTO.getPageNum(), tbCluePageDTO.getPageSize());
        Page<CluePageVo> page = tbClueMapper.findClue(tbCluePageDTO.getId(),
                tbCluePageDTO.getPhone(),
                tbCluePageDTO.getOwner(),
                tbCluePageDTO.getStatus(),
                tbCluePageDTO.getChannel(),
                beginCreateTime,
                endCreateTime);
        return page;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void ClueAdd(TbClueAddDTO tbClueAddDTO) {
        TbClue tbClue = new TbClue();
        BeanUtils.copyProperties(tbClueAddDTO, tbClue);
        tbClue.setStatus("1");
        tbClue.setCreateTime(LocalDateTime.now().toString());
        tbClue.setCreateBy(SecurityUtils.getUsername());
        tbClue.setUpdateTime(LocalDateTime.now().toString());
        tbClueMapper.insert(tbClue);

        SysUserDTO sysUserDTO = sysUserMapper.selectUserByUserName(tbClue.getCreateBy());
        if (tbClueAddDTO == null) {
            throw new BaseException("查找不到用户信息");
        }
        TbAssignRecord tbAssignRecord = new TbAssignRecord();
        tbAssignRecord.setAssignId(tbClue.getId());
        tbAssignRecord.setUserId(sysUserDTO.getUserId());
        tbAssignRecord.setUserName(sysUserDTO.getUserName());
        tbAssignRecord.setDeptId(sysUserDTO.getDeptId());
        tbAssignRecord.setCreateTime(tbClue.getCreateTime().toString());
        tbAssignRecord.setCreateBy(tbClue.getCreateBy());
        tbAssignRecord.setLatest("1");
        tbAssignRecord.setType("0");
        tbAssignRecordMapper.insert(tbAssignRecord);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void clueAssignment(ClueAssignmentDTO clueAssignmentDTO) {
        LambdaQueryWrapper<TbAssignRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(clueAssignmentDTO.getIds() != null, TbAssignRecord::getAssignId, clueAssignmentDTO.getIds());
        List<TbAssignRecord> tbAssignRecords = tbAssignRecordMapper.selectList(wrapper);
        if (tbAssignRecords == null && tbAssignRecords.size() == 0) {
            throw new BaseException("查找不到跟进记录");
        }

        LambdaQueryWrapper<SysUser> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(SysUser::getUserId, clueAssignmentDTO.getUserId());
        SysUser sysUser = sysUserMapper.selectOne(wrapper2);
        LambdaQueryWrapper<SysUser> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(SysUser::getUserId, clueAssignmentDTO.getUserId());
        Integer count = sysUserMapper.selectCount(wrapper1);
        if(count > 10){
            throw new BaseException("分配失败,最大分配数为(10)");
        }
        for (TbAssignRecord tbAssignRecord : tbAssignRecords) {
            tbAssignRecord.setUserId(sysUser.getUserId());
            tbAssignRecord.setUserName(sysUser.getUserName());
            tbAssignRecord.setDeptId(sysUser.getDeptId());
            tbAssignRecordMapper.updateById(tbAssignRecord);
        }
    }



    @Override
    public CluePageVo ClueAssign(Long id) {
        String userName = SecurityUtils.getUsername();
        CluePageVo cluePageVo = tbClueMapper.findClueAssign(id, userName);
        return cluePageVo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void falseClue(Long id, String reason, String remark) {
        TbClue tbClue = tbClueMapper.selectById(id);
        if(tbClue.getFalseCount() >= 3){
            throw new BaseException("不允许该线索再次变为伪线索");
        }
        tbClue.setStatus("3");
        tbClue.setFalseCount(tbClue.getFalseCount() + 1);
        tbClue.setUpdateTime(LocalDateTime.now().toString());
        tbClueMapper.updateById(tbClue);

        TbClueTrackRecord tbClueTrackRecord = new TbClueTrackRecord();
        tbClueTrackRecord.setClueId(id.toString());
        tbClueTrackRecord.setCreateBy(SecurityUtils.getUsername());
        tbClueTrackRecord.setCreateTime(LocalDateTime.now().toString());
        tbClueTrackRecord.setType("1");
        tbClueTrackRecord.setFalseReason(reason);

        tbClueTrackRecordMapper.insert(tbClueTrackRecord);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void clueGain(ClueAssignmentDTO clueAssignmentDTO) {
        LambdaQueryWrapper<TbClue> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(clueAssignmentDTO.getIds() != null, TbClue::getId, clueAssignmentDTO.getIds());
        List<TbClue> tbClues = tbClueMapper.selectList(wrapper);
        if(tbClues == null && tbClues.size() == 0){
            throw new BaseException("没有找到线索数据");
        }
        for (TbClue tbClue : tbClues) {
            tbClue.setStatus("2");
            tbClue.setUpdateTime(LocalDateTime.now().toString());
            LambdaQueryWrapper<TbClue> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(TbClue::getId,tbClue.getId());
            tbClueMapper.update(tbClue,wrapper1);
            TbClueTrackRecord tbClueTrackRecord = new TbClueTrackRecord();
            tbClueTrackRecord.setClueId(tbClue.getId().toString());
            tbClueTrackRecord.setCreateBy(SecurityUtils.getUsername());
            tbClueTrackRecord.setCreateTime(LocalDateTime.now().toString());
            tbClueTrackRecord.setType("0");
            tbClueTrackRecordMapper.insert(tbClueTrackRecord);
        }


    }

    @Override
    public Page<CluePageVo> findCluePoolList(TbCluePoolPageDTO tbCluePoolPageDTO, LocalDate beginCreateTime, LocalDate endCreateTime) {
        PageHelper.startPage(tbCluePoolPageDTO.getPageNum(), tbCluePoolPageDTO.getPageSize());
        Page<CluePageVo> page = tbClueMapper.findCluePoolList(tbCluePoolPageDTO.getId(),
                tbCluePoolPageDTO.getPhone(),
                tbCluePoolPageDTO.getChannel(),
                beginCreateTime,
                endCreateTime);
        return page;
    }


}




